package com.zzx.controller;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.zzx.po.Items;
import com.zzx.po.ItemsCustom;
import com.zzx.po.ItemsQueryVo;
import com.zzx.service.ItemsService;
import com.zzx.validation.ValidateGroup1;
import com.zzx.validation.ValidateGroup2;

@Controller
@RequestMapping("/items")
public class ItemsController {

	@Autowired
	private ItemsService itemsService;
	
	@ModelAttribute("modelItem")
	public Map<String, String> getMap() {
		Map<String, String> map = new HashMap();
		map.put("1", "电脑");
		map.put("2", "服装");
		return map;
	}
	
	@RequestMapping("/queryItems")
	public ModelAndView	queryItems() {
		List<Items> items = itemsService.findItemList(null);
		//List<Items> items = getItems();
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("itemsList",items);
		modelAndView.setViewName("itemsList");
		return modelAndView;
	}
	

	
//	@RequestMapping("/editItem")
//	public ModelAndView	editItem(@RequestParam(value = "id")Integer id) throws Exception {
//		ItemsCustom item = itemsService.findItemById(id);
//		ModelAndView modelAndView = new ModelAndView();
//		modelAndView.addObject("item",item);
//		modelAndView.setViewName("editItem");
//		return modelAndView;
//	}
	
	
	@RequestMapping("/editItem")
	public String	editItem(Model model,Integer id) throws Exception {
		ItemsCustom item = itemsService.findItemById(id);
		model.addAttribute("item",item);
		model.addAttribute("id",id);
		return "editItem";
	}
	
	@RequestMapping("/editItemSubmit")
	public String editItemSubmit(MultipartFile pictureFile, Model model, Integer id,
			@Validated(value=ValidateGroup2.class) @ModelAttribute("item") ItemsCustom itemsCustom, BindingResult bindingResult) throws Exception {
		if (bindingResult.hasErrors()) {
			List<ObjectError> allErrors = bindingResult.getAllErrors();
			model.addAttribute("errors",allErrors);
			for (ObjectError error : allErrors) {
				System.out.println(error.getDefaultMessage());
			}
			return "editItem";
		}
		if (pictureFile != null && pictureFile.getOriginalFilename() != null
				&& pictureFile.getOriginalFilename().length() != 0) {

			String originalFilename = pictureFile.getOriginalFilename();
			String uploadPath = "/Users/zzx/Desktop/gif/upload";
			String fileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
			File file = new File(uploadPath + "/" + fileName);
			pictureFile.transferTo(file);
			itemsCustom.setPic("upload/" + fileName);
		}

		itemsService.updateItem(id, itemsCustom);
		model.addAttribute("id", id);
		// model.addAttribute("item",itemsCustom);
		// return "redirect:queryItems.action";
		return "editItem";
	}
	
	
	@RequestMapping("/deleteItems")
	public String deleteItems(Integer[] delete_id) {
		System.out.println(delete_id);
		return "success";
	}
	
	@RequestMapping("viewItems/{id}")
	public @ResponseBody ItemsCustom viewItems(@PathVariable("id") Integer id) throws Exception{
		ItemsCustom itemsCustom = itemsService.findItemById(id);
		return itemsCustom;
	}
	
//	@InitBinder
//	public void initBinder(WebDataBinder binder) throws Exception {
//		binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));
//	}
	@RequestMapping("/editItems")
	public String	editItems(Model model) {
		List<Items> items = itemsService.findItemList(null);
		model.addAttribute("itemsList",items);
		return "editItems";
	}
	@RequestMapping("/editItemsSubmit")
	public String	editItemsSubmit(ItemsQueryVo vo) {
		System.out.println(vo);
		return "success";
	}
	
	
}
